package com.test.design.material.materialdesign;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.Collections;
import java.util.List;

import view.Utils;

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder>
        implements View.OnClickListener, View.OnLongClickListener ,ItemTouchHelperAdapterInterface {
    Context mContext;
    private List<String> dataList;
    private View convertView;
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    private OnRecyclerViewItemLongClickListener mOnItemLongClickListener = null;

    public ImageAdapter(Context context, List<String> dataList) {
        this.dataList = dataList;
        this.mContext = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        convertView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_image1, viewGroup, false);
        ViewHolder vh = new ViewHolder(convertView);
        //将创建的View注册点击事件
        convertView.setOnClickListener(this);
        convertView.setOnLongClickListener(this);
        return vh;
    }


    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
        viewHolder.itemView.setTag(position + "");
        viewHolder.item_recycler_text.setText(Utils.getInstance().setText(position));

    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick((String) v.getTag());
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (mOnItemLongClickListener != null) {
            mOnItemLongClickListener.onItemLongClick((String) v.getTag());
        }
        return true;
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public void setOnItemLongClickListener(OnRecyclerViewItemLongClickListener listener) {
        this.mOnItemLongClickListener = listener;
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    @Override
    public boolean onItemMove(int fromPosition, int toPosition) {
        // adapter 交换
        Collections.swap(dataList,fromPosition,toPosition);
        notifyItemMoved(fromPosition,toPosition);
        return false;
    }

    @Override
    public void onItemSwiped(int position) {
        dataList.remove(position);
        notifyItemRemoved(position);

    }

    @Override
    public void runLayoutAnimation(RecyclerView recyclerView) {

    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(String s);
    }

    public interface OnRecyclerViewItemLongClickListener {
        void onItemLongClick(String s);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView item_recycler_text;
        public ImageView item_recycler_img;

        public ViewHolder(View view) {
            super(view);
            item_recycler_text = view.findViewById(R.id.item_recycler_text);
            item_recycler_img = view.findViewById(R.id.item_recycler_img);


        }
    }

}